{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2747d921-3066-4ccc-a951-8d9ee5ebe843",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.5, 1.3, 7.5],\n",
       "       [5.6, 7.8, 1.2]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "liao_array = np.array([[1.5,1.3,7.5], [5.6,7.8,1.2]])\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5a7e6f16-5616-4fcf-8665-00d06f29cf6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.3, 1.5, 7.5],\n",
       "       [1.2, 5.6, 7.8]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(liao_array)  ###sort 排序 默认按照行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "706a63ce-0166-4815-a299-1cdf4d3e339b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.5, 1.3, 1.2],\n",
       "       [5.6, 7.8, 7.5]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(liao_array,axis=0)  ### 按照列排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4c078b12-26ea-46e8-b4a2-56ea7deb2c7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.5, 1.3, 7.5],\n",
       "       [5.6, 7.8, 1.2]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array ### 不改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5036e8be-ce36-415e-8614-f8a221de9716",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 2],\n",
       "       [2, 0, 1]], dtype=int64)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "90ff9b76-2c45-46f0-b5d4-e24a46424ebe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,\n",
       "        5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.linspace(0,10,10)  ### 从0 到 10 创建10个等间隔的数组\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "861263e0-0b11-4e96-ba4e-a2e6f095de20",
   "metadata": {},
   "outputs": [],
   "source": [
    "values = np.array([2.5,6.5,9.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3fc6332e-c2f7-4a8c-a1e2-86e08b91edd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 6, 9], dtype=int64)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.searchsorted(liao_array,values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dd4c8849-29c2-4765-a4d8-c5a8112f95e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 6],\n",
       "       [1, 7, 0],\n",
       "       [2, 3, 1],\n",
       "       [2, 4, 0]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.array([[1,0,6],\n",
    "                       [1,7,0],\n",
    "                       [2,3,1],\n",
    "                       [2,4,0]])\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "75c8fbab-a852-46cd-8d48-bf038ee928c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 2, 0], dtype=int64)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.lexsort([-1*liao_array[:,0], liao_array[:,2]])\n",
    "index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff8824d7-7d95-4444-a6f5-111682bf1590",
   "metadata": {},
   "source": [
    "lexsort函数表示对数组进行多列联合排序，根据多个键值列进行间接稳定排序，返回排序后的索引数组，优先按最后一个键排序，依次向前\n",
    "-1*表示降序排序\n",
    "排序过程： 1、liao_arary[:,2] 先让第二列升序排列\n",
    "          2、-1*liao_array[:,0] 再让组内第0列降序排列（什么叫组内？就是第2列元素相同的，就是一组）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "35051554-41f0-444a-b421-2d25a9641076",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4, 0],\n",
       "       [1, 7, 0],\n",
       "       [2, 3, 1],\n",
       "       [1, 0, 6]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = liao_array[index]\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "068fb3e8-9a25-448e-9ab0-f9f9c3400f8e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
